﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace l1cs.server.model.skill
{
    class L1SkillTimerThreadImpl : L1SkillTimer
    {
        public L1SkillTimerThreadImpl( L1Character cha, int skillId, int timeMillis )
        {
            _cha = cha;
            _skillId = skillId;
            _timeMillis = timeMillis;
        }

        bool stopflg = false;
        public void L1SkillTimerThreadSub( Object state )
        {
            for ( int timeCount = _timeMillis / 1000; timeCount > 0; timeCount-- )
            {
                Thread.Sleep( 1000 );
                _remainingTime = timeCount;
                if ( stopflg )
                    break;
            }
            _cha.removeSkillEffect( _skillId );
        }

        public int getRemainingTime()
        {
            return _remainingTime;
        }

        public void begin()
        {
            //GeneralThreadPool.getInstance().execute(this);
            ThreadPool.QueueUserWorkItem( new WaitCallback( L1SkillTimerThreadSub ) );
        }

        public void end()
        {
            //base.interrupt();
            stopflg = true;
            L1SkillStop.stopSkill( _cha, _skillId );
        }

        public void kill()
        {
            //if ( Thread.currentThread().getId() == base.getId() )
            //{
            //    return; // 呼び出し元スレッドが自分であれば止めない
            //}
            stopflg = true;
        }

        private L1Character _cha;
        private int _timeMillis;
        private int _skillId;
        private int _remainingTime;
    }
}
